<?php
require_once 'web.php';

/**
 * Description of SearchController
 *
 * @author Ecky
 */
class SearchController extends WebController {

    private $thumbperpage = 6;

    public function index($query = null) {
        //Cheap query handling kalo nerima langsung dari search bar
        //Ga tau cara ngubah jadi clean URL
        if($query == null && isset ($_POST['query'])) $query = $_POST['query'];

        //Redirect ke photos defaultnya
        URL::redirect("search/photos/0/$query");
    }

    public function users($page = null, $query = null) {
        //Do the searching to user (uname or name)
        $thumbperpage = $this->thumbperpage;
        $photos = new ORM('user');
        $qquery = mysql_real_escape_string($query);
        $photos = $photos->query("select * from user where name like '%$qquery%' OR username like '%$qquery%'");

        $startIdx = $page * $thumbperpage;
        $totalRes = count($photos);
        $endIdx = min($page * $thumbperpage + $thumbperpage, $totalRes);
        $photos = array_slice($photos, $startIdx, $endIdx - $startIdx);
        
        ///Set the view
        $this->view->content = View::factory("search/index");
        $this->cleanTab($page, $query);
        $this->view->content->query = $query;
        $this->view->content->searchtype = "users";
        $this->view->content->page = $page;
        $this->view->content->startIdx = $startIdx + 1;
        $this->view->content->endIdx = $endIdx;
        $this->view->content->totalRes = $totalRes;
        $this->view->content->thumbfactoryname = "search/userthumbnail";
        $this->view->content->models = $photos;
    }

    public function photos($page = null, $query = null) {
        //Do the searching to photos (keywords)
        $thumbperpage = $this->thumbperpage;
        $photos = new ORM('photo');
        $qquery = mysql_real_escape_string($query);
        $photos = $photos->query("select * from photo where keyword like '%$qquery%' OR description like '%$qquery%'");

        //Filter photos by access type
        $passed = array();
        foreach ($photos as $photo) {
            //If this is my photo, pass it
            if($this->auth_user->is_login() && $photo->owner == $this->auth_user->user->id) {
                $passed[] = $photo;
                continue;
            }

            //if access type self, ignore
            if($photo->view_access == 4) continue;

            //If public, passes and continue
            if($photo->view_access == 1) { $passed[] = $photo; continue; }

            //if login, check user login and continue
            if($photo->view_access == 2 && $this->auth_user->is_login()) {
                $passed[] = $photo;
                continue;
            }
            
            //If following, check if this user is following..
            if($photo->view_access == 3 && $this->auth_user->is_login()) {
                $follow = new ORM('followings');
                $follow->where("`followee` = $photo->owner AND `follower` = " . $this->auth_user->user->id);
                $follow->find();
                if($follow->id != null);
                    $passed[] = $photo;
                continue;
            }
        }
        $photos = $passed;
        
        $startIdx = $page * $thumbperpage;
        $totalRes = count($photos);
        $endIdx = min($page * $thumbperpage + $thumbperpage, $totalRes);
        $photos = array_slice($photos, $startIdx, $endIdx - $startIdx);

        ///Set the view
        $this->view->content = View::factory("search/index");
        $this->cleanTab($page, $query);
        $this->view->content->query = $query;
        $this->view->content->searchtype = "photos";
        $this->view->content->page = $page;
        $this->view->content->startIdx = $startIdx + 1;
        $this->view->content->endIdx = $endIdx;
        $this->view->content->totalRes = $totalRes;
        $this->view->content->thumbfactoryname = "search/photothumbnail";
        $this->view->content->models = $photos;
    }

    private function cleanTab($page, $query) {
        //Method and params escape
        if(!isset ($query)) $qquery = "/";
        else $qquery = "/" . $query;
        if(!isset ($page)) $ppage = "/0";
        else $ppage = "/" . $page;

        //Set home links
        $home_links = array(
            "search/photos$ppage$qquery" => array("active" => false, "value" => "Photos"),
            "search/users$ppage$qquery" => array("active" => false, "value" => "Users"),
        );
        $home_links[$this->router->controller . "/" . $this->router->method . $ppage . $qquery]['active'] = true;

        $this->view->content->home_links = $home_links;
    }
}
?>
